package com.lzy.crm.wechat.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzy.crm.base.page.PageQuery;
import com.lzy.crm.base.util.BootStrapReturn;
import com.lzy.crm.base.util.JsonUtil;
import com.lzy.crm.base.util.StringUtil;
import com.lzy.crm.domain.Crowd;
import com.lzy.crm.domain.CrowdApply;
import com.lzy.crm.domain.CrowdFlow;
import com.lzy.crm.domain.CrowdUser;
import com.lzy.crm.domain.User;
import com.lzy.crm.service.CrowdApplyService;
import com.lzy.crm.service.CrowdFlowService;
import com.lzy.crm.service.CrowdService;
import com.lzy.crm.service.CrowdUserService;
import com.lzy.crm.service.UserService;
import com.lzy.crm.util.BootJsonResponse;
import com.lzy.crm.vo.PlayerInfo;
import com.lzy.crm.wechat.consts.CrowdConstants;
import com.lzy.crm.wechat.consts.WechatPayProperties;
import com.lzy.crm.wechat.util.WeixinUtil;

/**
 * 牌友圈 功能
 * @author csp
 *
 */
@Controller
@RequestMapping("/wechat")
public class CrowdController {
	
	Logger logger= Logger.getLogger(CrowdController.class);
	
	@Resource
	private CrowdService crowdService;
	
	@Resource
	private CrowdUserService crowdUserService;
	
	@Resource
	private CrowdApplyService crowdApplyService;
	
	@Resource
	private UserService userService;
	
	@Resource
	private CrowdFlowService crowdFlowService;
	
	
	
	@RequestMapping("/crowd_index") 
	public String crowd_index2(HttpServletRequest request ,ModelMap model) {
		User user =(User)request.getSession().getAttribute("userSession");
		// 登陆
		if(user==null||StringUtil.isEmpty(user.getOpenid())
				||StringUtil.isEmpty(user.getUnionid())
				){
			// 调用微信登陆
			model.put("appid", WechatPayProperties.getAppId());
			model.put("redirectUri", WechatPayProperties.getRedirectUri()+"?page=wechat/crowd_index.html");
			return "wechat/index2"; 
		}
		PlayerInfo playerInfo=userService.getPlayerInfo(null, user.getUnionid());
		if(playerInfo==null){
			model.put("result", 1);
			return "wechat/crowd/crowd_index";
		}
		request.getSession().setAttribute("playerSession", playerInfo);
		return "wechat/crowd/crowd_index";
		
		
		/*
		try {
			System.out.println("unionid="+unionid);  
			PlayerInfo playerInfo=userService.getPlayerInfo(null, unionid);
			if(playerInfo==null){
				model.put("result", 1);
				return "wechat/crowd/crowd_index";
			}
			request.getSession().setAttribute("userSession", playerInfo);
		} catch (Exception e) {
			logger.error(e);
		}
		return "wechat/crowd/crowd_index";*/
	}
	
	
	/**
	 * 我的牌友群
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/crowd_my") 
	public String crowd_my(HttpServletRequest request, ModelMap model) {
		PlayerInfo playerInfo = (PlayerInfo)request.getSession().getAttribute("playerSession");
		List<Crowd> crowdList=crowdService.getMyCrowdListByUnionId(playerInfo.getUnionid());
		model.put("crowdList", crowdList);
		return "wechat/crowd/crowd_my";
	}
	
	/**
	 * 我加入的牌友圈
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/crowd_join") 
	public String crowd_jion(HttpServletRequest request, ModelMap model) {
		PlayerInfo playerInfo = (PlayerInfo)request.getSession().getAttribute("playerSession");
		List<Crowd> crowdList=crowdService.getJoinCrowdListByUnionId(playerInfo.getUnionid());
		for(Crowd crowd :crowdList){
			CrowdUser crowdUser=crowdUserService.getCrowdUserByCrowdIdAndGameId(crowd.getId().toString(), playerInfo.getGameId().toString());
			crowd.setStatus(crowdUser.getIsCard());
		}
		model.put("crowdList", crowdList);
		return "wechat/crowd/crowd_join";
	}
	
	/**
	 * 我的牌友圈
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/crowd_player") 
	public String crowd_player(HttpServletRequest request, ModelMap model) {
		String crowdId = request.getParameter("crowdId");
		Crowd crowd = crowdService.getById(crowdId);
		PlayerInfo playerInfo=userService.getPlayerInfo(null, crowd.getUnionid());
		model.put("playerInfo", playerInfo);
		model.put("crowd", crowd);
		model.put("crowdId", crowdId);
		return "wechat/crowd/crowd_player";
	}
	
	/**
	 * 我的牌友圈
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/crowd_join_player") 
	public String crowd_join_player(HttpServletRequest request, ModelMap model) {
		String crowdId = request.getParameter("crowdId");
		PlayerInfo sessionPlayerInfo = (PlayerInfo)request.getSession().getAttribute("playerSession");
		Crowd crowd = crowdService.getById(crowdId);
		PlayerInfo playerInfo = userService.getPlayerInfo(null, crowd.getUnionid());
		CrowdUser crowdUser = crowdUserService.getCrowdUserByCrowdIdAndGameId(crowdId, sessionPlayerInfo.getGameId().toString());
		model.put("crowdUser", crowdUser);
		model.put("playerInfo", playerInfo);
		model.put("crowd", crowd);
		model.put("crowdId", crowdId);
		return "wechat/crowd/crowd_join_player";
	}
	
	/**
	 * 群成员列表
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(path="/crowd_player_data",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String crowd_player_data(HttpServletRequest request, @RequestParam Map<String, String> form,
			ModelMap model) {
		BootStrapReturn<CrowdUser> result = new BootStrapReturn<CrowdUser>();
		try {
			String gameId = request.getParameter("gameId");
			String crowdId = request.getParameter("crowdId");
			Integer currentPage = Integer.parseInt(request.getParameter("numPerPage"));
			Integer numPerPage = Integer.parseInt(request.getParameter("pageNum"));
			PageQuery pageQuery = new PageQuery(numPerPage,currentPage);
			pageQuery.setIsQueryTotal(true); 
			List<CrowdUser> crowdUserList = crowdUserService.getCrowdUserList(crowdId, gameId, pageQuery);
			result.setPage(pageQuery.getPageIndex());
			result.setTotal(pageQuery.getTotalCount());
			result.setRows(crowdUserList);
		} catch (Exception e) {
			logger.error("群成员列表", e);
		}
		return JsonUtil.toJson(result) ;
	}
	
	
	/**
	 * 增加牌友圈
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/crowd_add") 
	public String crowd_add(HttpServletRequest request, ModelMap model) {
		return "wechat/crowd/crowd_add";
	}
	
	
	
	/**
	 * 创群信息保存
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(path="/crowd_add_save",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String doEditUser(HttpServletRequest request,ModelMap model) {
		BootJsonResponse bootJsonResponse =new BootJsonResponse();
		try {
			PlayerInfo playerInfo = (PlayerInfo)request.getSession().getAttribute("playerSession");
			String crowdName = request.getParameter("crowdName");
			String remark = request.getParameter("remark");
			// 创建牌友圈
			/*int result = crowdService.addCrowd(crowdName, remark,  playerInfo);
			if(result==1){
				bootJsonResponse.setMessage("操作成功");
			}else if(result==2){
				bootJsonResponse.setMessage("该功能只对代理开放~");
			}else{
				bootJsonResponse.setMessage("您创建的牌圈数量已达最大数量，请先删除没用的牌圈后再试~");
			}*/
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			bootJsonResponse.setMessage("操作失败，请联系管理员");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			logger.error("修改个人信息失败", e);
		}
		return JsonUtil.toJson(bootJsonResponse);
	}
	
	
	/**
	 * 管理员权限增加牌友圈
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/crowd_add_message") 
	public String crowd_add_message(HttpServletRequest request, ModelMap model) {
		return "wechat/crowd/crowd_add_message";
	}
	
	
	
	/**
	 * 管理员帮助玩家创群信息保存
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(path="/crowd_add_save_message",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String crowd_add_message_save(HttpServletRequest request,ModelMap model) {
		BootJsonResponse bootJsonResponse =new BootJsonResponse();
		try {
			String gameId = request.getParameter("gameId");
			PlayerInfo playerInfo = userService.getPlayerInfo(Integer.valueOf(gameId), null) ;
			if(playerInfo==null){
				bootJsonResponse.setMessage("查不到玩家信息,创建失败~");
				bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			}
			String crowdName = request.getParameter("crowdName");
			String remark = request.getParameter("remark");
			// 创建牌友圈
			int result = crowdService.addCrowdByMessage(crowdName, remark,  playerInfo);
			if(result==1){
				bootJsonResponse.setMessage("操作成功");
			}
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			bootJsonResponse.setMessage("操作失败，请联系管理员");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			logger.error("修改个人信息失败", e);
		}
		return JsonUtil.toJson(bootJsonResponse);
	}
	
	
	
	@RequestMapping(path="/crowdUser_add_save",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String crowdUser_add_save(HttpServletRequest request,ModelMap model) {
		BootJsonResponse bootJsonResponse =new BootJsonResponse();
		try {
			String crowdId = request.getParameter("crowdId");
			String gameId = request.getParameter("crowdgameId");
			String remark = request.getParameter("remark");
			if(crowdUserService.getCrowdUser(crowdId, gameId)){
				bootJsonResponse.setMessage(" 该玩家已是俱乐部成员~ ");
				bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
				return JsonUtil.toJson(bootJsonResponse);
			}
			PlayerInfo playerInfo = userService.getPlayerInfo(Integer.parseInt(gameId),null);
			// 创建牌友圈
			//crowdUserService.addCrowdUserByPlayerInfo(crowdId,remark, playerInfo);
			bootJsonResponse.setMessage("操作成功");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			bootJsonResponse.setMessage("操作失败，请联系管理员");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			logger.error("修改个人信息失败", e);
		}
		return JsonUtil.toJson(bootJsonResponse);
	}
	
	/**
	 * 移除群玩家
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path="/crowdUser_del",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String crowdUser_del(HttpServletRequest request,ModelMap model) {
		BootJsonResponse bootJsonResponse =new BootJsonResponse();
		try {
			String crowdUserId = request.getParameter("crowdUserId");
			CrowdUser crowdUser = crowdUserService.getById(crowdUserId);
			// 删除牌友群 玩家
			crowdUserService.deleteByPrimaryKey(crowdUserId);
			List<CrowdUser> crowdUseList = crowdUserService.getCrowdUserList(crowdUser.getGameId().toString());
			boolean isCard = true;
			for(CrowdUser crowdUserbf:crowdUseList){
				if(StringUtil.equals("1", crowdUserbf.getIsCard())){
					isCard = false;
				}
			}
			//没有默认牌圈
			if(isCard){
				if(crowdUseList.size()>0){
					CrowdUser defCrowdUser = crowdUseList.get(0);
					defCrowdUser.setIsCard("1");
					crowdUserService.updateByPrimaryKeySelective(defCrowdUser);
				}
			}
			
			crowdUserService.updateCrowdUserCount(crowdUser.getCrowdId().toString());
			bootJsonResponse.setMessage("操作成功");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			bootJsonResponse.setMessage("操作失败，请联系管理员");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			logger.error("修改个人信息失败", e);
		}
		return JsonUtil.toJson(bootJsonResponse);
	}
	
	/**
	 * 删除牌友群
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path="/crowd_del",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String crowd_del(HttpServletRequest request,ModelMap model) {
		BootJsonResponse bootJsonResponse =new BootJsonResponse();
		try {
			String crowdId = request.getParameter("crowdId");
			//删除牌友群关系
			//crowdUserService.delCrowdUserByCrowdId(crowdId);
			//删除牌友群
			Crowd crowd = crowdService.getById(crowdId);
			// 设置删除状态
			crowd.setStatus(CrowdConstants.crowd_status_del);
			crowdService.updateByPrimaryKeySelective(crowd);
			bootJsonResponse.setMessage("操作成功");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			bootJsonResponse.setMessage("操作失败，请联系管理员");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			logger.error("修改个人信息失败", e);
		}
		return JsonUtil.toJson(bootJsonResponse);
	}
	
	
	
	/**
	 * 查找牌友群
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/crowd_find") 
	public String crowd_find(HttpServletRequest request, ModelMap model) {
		return "wechat/crowd/crowd_find";
	}
	
	/**
	 * 查找群信息
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(path="/crowd_find_data",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String crowd_find_data(HttpServletRequest request,ModelMap model) throws Exception{
		try{
			Integer crowdId =Integer.parseInt(request.getParameter("crowdId"));
			Crowd crowd = crowdService.getById(crowdId);
			if(crowd!=null){
				model.put("crowd", crowd);
			}
			model.put("statusCode", BootJsonResponse.STATUS_CODE_SUCCESS);
		}catch(Exception e){
			logger.error("查询单个牌友群错误", e);
			model.put("statusCode", BootJsonResponse.STATUS_CODE_FAILURE);
		}
		return JsonUtil.toJson(model);
	}
	
	@RequestMapping(path="/crowd_find_save",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String crowd_find_save(HttpServletRequest request,ModelMap model) throws Exception{
		try{
			Integer crowdId =Integer.parseInt(request.getParameter("crowdId"));
			String remark = request.getParameter("remark");
			PlayerInfo playerInfo = (PlayerInfo)request.getSession().getAttribute("playerSession");
			model.put("statusCode", BootJsonResponse.STATUS_CODE_SUCCESS);
			if(playerInfo ==null){
				model.put("msg", "请先下载游戏才能申请加入该俱乐部~");
				return JsonUtil.toJson(model);
			}
			
			if( crowdUserService.getCrowdUser(String.valueOf(crowdId),String.valueOf(playerInfo.getGameId()))){
				model.put("msg", "您已经是该俱乐部成员~");
				return JsonUtil.toJson(model);
			}
			/*Integer result = crowdApplyService.applyCrowd(crowdId, remark, playerInfo);
			if(result==0){
				model.put("msg", "数据错误~");
			}else if(result==1){
				model.put("msg", " 申请已提交，请通知群主在俱乐部管理里点同意即可~ ");
			}else if(result==2){
				model.put("msg", " 申请已提交，请通知群主在俱乐部管理里点同意即可~ ");
			}*/
		}catch(Exception e){
			logger.error("查询单个牌友俱乐部错误", e);
			model.put("statusCode", BootJsonResponse.STATUS_CODE_FAILURE);
		}
		return JsonUtil.toJson(model);
	}
	
	
	
	/**
	 * 退出牌友群
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path="/crowd_exit",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String crowd_exit(HttpServletRequest request,ModelMap model) {
		BootJsonResponse bootJsonResponse =new BootJsonResponse();
		try {
			PlayerInfo playerInfo = (PlayerInfo)request.getSession().getAttribute("playerSession");
			String crowdId = request.getParameter("crowdId");
			//删除牌友群关系
			crowdUserService.delCrowdUserByCrowdIdAndGameId(crowdId, playerInfo.getGameId().toString());
			
			List<CrowdUser> crowdUseList = crowdUserService.getCrowdUserList(playerInfo.getGameId().toString());
			boolean isCard = true;
			for(CrowdUser crowdUserbf:crowdUseList){
				if(StringUtil.equals("1", crowdUserbf.getIsCard())){
					isCard = false;
				}
			}
			//没有默认牌圈
			if(isCard){
				if(crowdUseList.size()>0){
					CrowdUser defCrowdUser = crowdUseList.get(0);
					defCrowdUser.setIsCard("1");
					crowdUserService.updateByPrimaryKeySelective(defCrowdUser);
				}
			}
			
			crowdUserService.updateCrowdUserCount(crowdId);
			bootJsonResponse.setMessage("操作成功");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			bootJsonResponse.setMessage("操作失败，请联系管理员");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			logger.error("修改个人信息失败", e);
		}
		return JsonUtil.toJson(bootJsonResponse);
	}
	
	
	/**
	 * 群消息
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/crowd_notice") 
	public String crowd_notice(HttpServletRequest request, ModelMap model) {
		PlayerInfo playerInfo = (PlayerInfo)request.getSession().getAttribute("playerSession");
		String gameId = playerInfo.getGameId().toString();
		List<CrowdApply> crowdApplyList = crowdApplyService.getCrowdApplyListByGameId(gameId);
		List<CrowdApply> myCrowdApplyList = crowdApplyService.getMyCrowdApplyListByGameId(gameId);
		model.put("crowdApplyList", crowdApplyList);
		model.put("myCrowdApplyList", myCrowdApplyList);
		return "wechat/crowd/crowd_notice";
	}
	
	/**
	 * 保持牌友圈状态
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path="/crowd_apply_edit",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String crowd_apply_edit(HttpServletRequest request,ModelMap model) {
		BootJsonResponse bootJsonResponse =new BootJsonResponse();
		try {
			PlayerInfo playerInfo = (PlayerInfo)request.getSession().getAttribute("playerSession");
			String crowdApplyId = request.getParameter("crowdApplyId");
			String status = request.getParameter("status");
			String remark = request.getParameter("remark");
			// 修改申请信息
			//crowdApplyService.updateCrowdApply(crowdApplyId, status, remark,playerInfo);
			bootJsonResponse.setMessage("操作成功");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			bootJsonResponse.setMessage("操作失败，请联系管理员");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			logger.error("修改个人信息失败", e);
		}
		return JsonUtil.toJson(bootJsonResponse);
	} 
	
	
	/**
	 * 创群信息保存
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(path="/crowd_user_default",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String crowd_user_default(HttpServletRequest request,ModelMap model) {
		BootJsonResponse bootJsonResponse =new BootJsonResponse();
		try {
			PlayerInfo playerInfo = (PlayerInfo)request.getSession().getAttribute("playerSession");
			String crowdId = request.getParameter("crowdId");
			String status = request.getParameter("status");
			// 创建牌友圈
			crowdUserService.updateCrowdStatusDefault(playerInfo, crowdId, status);
			bootJsonResponse.setMessage("操作成功");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			bootJsonResponse.setMessage("操作失败，请联系管理员");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			logger.error("修改个人信息失败", e);
		}
		return JsonUtil.toJson(bootJsonResponse);
	}
	
	
	
	/*********  牌友房卡记录信息查询   ************/
	/**
	 * 查询群所以记录
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/crowd_flow") 
	public String crowd_flow(HttpServletRequest request, ModelMap model) {
		PlayerInfo playerInfo = (PlayerInfo)request.getSession().getAttribute("playerSession");
		model.put("playerInfo", playerInfo);
		return "wechat/crowd/crowd_flow";
	}
	
	/**
	 * 查询群正在进行的游戏玩家
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/crowd_flow_ing") 
	public String crowd_flow_ing(HttpServletRequest request, ModelMap model) {
		PlayerInfo playerInfo = (PlayerInfo)request.getSession().getAttribute("playerSession");
		model.put("playerInfo", playerInfo);
		return "wechat/crowd/crowd_flow_ing";
	}
	
	/**
	 * 查询某个群的消耗记录
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/crowd_flow_crowd_id") 
	public String crowd_flow_crowdId(HttpServletRequest request, ModelMap model) {
		String crowdId = request.getParameter("crowdId");
		PlayerInfo playerInfo = (PlayerInfo)request.getSession().getAttribute("playerSession");
		Crowd crowd = crowdService.getById(crowdId);
		CrowdUser crowdUser = crowdUserService.getCrowdUserByCrowdIdAndGameId(crowdId, playerInfo.getGameId().toString());
		model.put("crowdUser", crowdUser);
		model.put("playerInfo", playerInfo);
		model.put("crowd", crowd);
		model.put("crowdId", crowdId);
		return "wechat/crowd/crowd_flow_crowd_id";
	}
	
	/**
	 * 群成员列表
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(path="/crowd_flow_data",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String crowd_flow_data(HttpServletRequest request, @RequestParam Map<String, String> form,
			ModelMap model) {
		BootStrapReturn<CrowdFlow> result = new BootStrapReturn<CrowdFlow>();
		PlayerInfo playerInfo = (PlayerInfo)request.getSession().getAttribute("playerSession");
		try {
			String gameId = request.getParameter("gameId");
			String crowdId = request.getParameter("crowdId");
			String startTime = request.getParameter("startTime");
			String endTime = request.getParameter("endTime");
			// gameOver 查询未完成的游戏
			String gameOver =request.getParameter("gameOver");
			Integer currentPage = Integer.parseInt(request.getParameter("numPerPage"));
			Integer numPerPage = Integer.parseInt(request.getParameter("pageNum"));
			PageQuery pageQuery = new PageQuery(numPerPage,currentPage);
			pageQuery.setIsQueryTotal(true); 
			List<CrowdFlow> crowdFlowList = crowdFlowService.getCrowdFlowList(playerInfo.getUnionid(),crowdId, gameId, startTime, endTime, gameOver,pageQuery);
			result.setPage(pageQuery.getPageIndex());
			result.setTotal(pageQuery.getTotalCount());
			result.setRows(crowdFlowList);
		} catch (Exception e) {
			logger.error("群成员消耗房卡列表", e);
		}
		return JsonUtil.toJson(result) ;
	}
	
	/**
	 * 移除群玩家
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path="/crowdUser_reset",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String crowdUser_reset(HttpServletRequest request,ModelMap model) {
		BootJsonResponse bootJsonResponse =new BootJsonResponse();
		PlayerInfo playerInfo = (PlayerInfo)request.getSession().getAttribute("playerSession");
		try {
			String crowdUserId = request.getParameter("crowdUserId");
			CrowdUser crowdUser = crowdUserService.getById(crowdUserId);
			Crowd crowd = crowdService.getById(crowdUser.getCrowdId());
			// 判断是否管理该群
			if(StringUtil.equals(crowd.getUnionid(), playerInfo.getUnionid())){
				crowdUser.setUseNub(0);
				crowdUser.setUpdateTime(new Date());
				crowdUserService.updateByPrimaryKeySelective(crowdUser);
				bootJsonResponse.setMessage("操作成功");
				bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
			}else{
				bootJsonResponse.setMessage("不是群成员~清零失败");
				bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			}
		} catch (Exception e) {
			bootJsonResponse.setMessage("操作失败，请联系管理员");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			logger.error("群主房卡清零失败", e);
		}
		return JsonUtil.toJson(bootJsonResponse);
	}
	
	
	
	/**
	 * 修改牌友圈
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/crowd_edit") 
	public String crowd_edit(HttpServletRequest request, ModelMap model) {
		String crowdId = request.getParameter("crowdId");
		Crowd crowd = crowdService.getById(crowdId);
		model.put("crowd", crowd);
		return "wechat/crowd/crowd_edit";
	}
	
	
	
	/**
	 * 修改信息保存
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(path="/crowd_edit_save",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String crowd_edit_save(HttpServletRequest request,ModelMap model) {
		BootJsonResponse bootJsonResponse =new BootJsonResponse();
		try {
			String crowdId = request.getParameter("crowdId");
			String crowdName = request.getParameter("crowdName");
			String remark = request.getParameter("remark");
			// 创建牌友圈
			Crowd crowd=crowdService.getById(crowdId);
			crowd.setCrowdName(crowdName);
			crowd.setRemark(remark);
			crowdService.updateByPrimaryKeySelective(crowd);
			bootJsonResponse.setMessage("操作成功");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			bootJsonResponse.setMessage("操作失败，请联系管理员");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			logger.error("修改个人信息失败", e);
		}
		return JsonUtil.toJson(bootJsonResponse);
	}
	
	/*************     牌圈邀请功能  begin         *****************/
	
	/**
	 * 牌圈邀请
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/crowd_invite") 
	public String crowd_index(HttpServletRequest request ,ModelMap model) {
		String crowdId = request.getParameter("crowdId");
		String gameId = request.getParameter("gameId");
		model.put("invite", false);
		boolean isweixin = WeixinUtil.isWeiXin(request);
		if(isweixin){
			User user =(User)request.getSession().getAttribute("userSession");
			// 登陆
			if(user==null||StringUtil.isEmpty(user.getOpenid())
					||StringUtil.isEmpty(user.getUnionid())
					){
				// 调用微信登陆
				model.put("appid", WechatPayProperties.getAppId());
				model.put("redirectUri", WechatPayProperties.getRedirectUri()+"?page=wechat/crowd_invite.html?crowdId="+crowdId+"&gameId="+gameId);
				return "wechat/index2"; 
			}
			PlayerInfo playerInfo = userService.getPlayerInfo(null, user.getUnionid());
			request.getSession().setAttribute("playerSession",playerInfo);
			if(StringUtil.equals(gameId, user.getGameUserId().toString())){
				model.put("invite", true);
			}
		}
		Crowd crowd = crowdService.getById(crowdId);
		User userCrowd =userService.getUserByUnionid(crowd.getUnionid());
		model.put("crowd", crowd);
		model.put("userCrowd", userCrowd);
		return "wechat/crowd/crowd_invite";
	}
	
	
	@RequestMapping(path="/crowd_invite_save",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String crowd_invite_save(HttpServletRequest request,ModelMap model) throws Exception{
		try{
			Integer crowdId =Integer.parseInt(request.getParameter("crowdId"));
			String remark = request.getParameter("remark");
			model.put("statusCode", BootJsonResponse.STATUS_CODE_SUCCESS);
			PlayerInfo playerInfo = (PlayerInfo)request.getSession().getAttribute("playerSession");
			if(playerInfo ==null){
				model.put("msg", "请先下载游戏才能申请加入俱乐部~");
				return JsonUtil.toJson(model);
			}
			
			if( crowdUserService.getCrowdUser(String.valueOf(crowdId),String.valueOf(playerInfo.getGameId()))){
				model.put("msg", "您已经是该俱乐部成员~");
				return JsonUtil.toJson(model);
			}
			/*Integer result = crowdApplyService.applyCrowd(crowdId, remark, playerInfo);
			if(result==0){
				model.put("msg", "数据错误~");
			}else if(result==1){
				model.put("msg", " 申请已提交，请通知群主在俱乐部管理里点同意即可~ ");
			}else if(result==2){
				model.put("msg", " 申请已提交，请通知群主在俱乐部管理里点同意即可~ ");
			}*/
		}catch(Exception e){
			logger.error("查询单个俱乐部错误", e);
			model.put("statusCode", BootJsonResponse.STATUS_CODE_FAILURE);
		}
		return JsonUtil.toJson(model);
	}
	
	
	/****************** 牌圈邀请功能  *******************/

}
